package edu.utd.text.game.controller.commands;

import java.util.ArrayList;

import edu.utd.text.game.controller.Controller;
import edu.utd.text.game.domain.GameObject;
import edu.utd.text.game.domain.exceptions.GameObjectNotFoundException;

/**
 * Examining a game object 
 * should invoke its getDescription method.
 * @author troy
 *
 */

public class ExamineCommand implements GameCommand {
	//a string representation (from user input) of the object
	//we are trying to examine
	private String objectToExamine;
	
	public ExamineCommand() {
		this("");
	}
	
	public ExamineCommand(String objectOfInterest) {
		this.objectToExamine = objectOfInterest;
	}
	
	public String getObjectToExamine() { return objectToExamine; }
	
	public void setObjectToExamine(String objectOfInterest) { this.objectToExamine = objectOfInterest; }
	
	
	public String execute() {
		//if there is an object (target) assoc. w/ this command,
		//try to find it in the current room
		Controller controller = Controller.INSTANCE;
		
		if (null != objectToExamine && !objectToExamine.isEmpty()) {
			ArrayList<GameObject> objectsInRoom = controller.getPlayer().getCurrentRoom().getObjects();
			
			//look for the object in the room
			for (GameObject gameObject : objectsInRoom) {
				if (gameObject.tagsMatch(objectToExamine)) {
					return gameObject.getDescription();
				}
			}
			//look for the object in player inventory
			try {
				return controller.getPlayer().getInventory().getItem(objectToExamine).getDescription();
			} 
			catch (GameObjectNotFoundException gonfx) {
				; // fall thru to the return
			}
			return "I don't see a \"" + objectToExamine + "\" here...";
		}
		else {
			//if there was no object (target) of the exam command, exam the room
			return controller.getPlayer().getCurrentRoom().getDescription();
		}
	}
}
